package com.czl.module_service.viewmodel

import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.TaskUserBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 * @author by xubin,
 * @date on 2022/3/4.
 * PS: Not easy to write code, please indicate.
 */
open class ApprovalFlowViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    val changeEvent = UiChangeEvent()

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<List<TaskUserBean>?> = SingleLiveEvent()
        val loadTaskStatusEvent: SingleLiveEvent<TaskUserBean?> = SingleLiveEvent()
    }

    fun getTaskUserByRelationId(businessId: Int, relationId: Int) {
        model.mTengYunHttpDataSource
            .getTaskUserByRelationId(
                GsonUtils.toJson(
                    mapOf(
                        "businessId" to businessId,
                        "relationId" to relationId
                    )
                )
            ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<TaskUserBean>>>() {
                override fun onResult(t: BaseBean<List<TaskUserBean>>) {
                    if (t.state == 0) {
                        changeEvent.loadCompleteEvent.postValue(t.data)
                    }
                    dismissLoading()
                }
                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }

    fun getTaskStatus(businessId: Int, relationId: Int) {
        val arrayListOf = arrayListOf<String>("" + relationId + "_" + businessId)
        model.mTengYunHttpDataSource
            .searchUsers(
                GsonUtils.toJson(
                    mapOf(
                        "businessId" to businessId,
                        "relationIdList" to arrayListOf
                    )
                )
            ).compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<TaskUserBean>>() {
                override fun onResult(t: BaseBean<TaskUserBean>) {
                    if (t.state == 0) {
                        changeEvent.loadTaskStatusEvent.postValue(t.data)
                    }
                    dismissLoading()
                }
                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}